package com.ding.easycontroller.service.impl;

import com.ding.easycontroller.dao.ServiceDao;
import com.ding.easycontroller.exception.CheckException;
import com.ding.easycontroller.service.ServiceService;
import com.ding.easycontroller.utils.IdStrategy;
import com.ding.easycontroller.work.Service;
import org.springframework.beans.factory.annotation.Autowired;

import javax.script.ScriptEngine;
import java.util.ArrayList;
import java.util.List;

@org.springframework.stereotype.Service
public class ServiceServiceImpl implements ServiceService {


    @Autowired
    private ServiceDao serviceDao;
    @Autowired
    private ScriptEngine scriptEngine;

    private List<Service> services = new ArrayList<>();

    private Service getByName(String name) {
        for (Service service : services) {
            if (service.getName().equals(name)) {
                return service;
            }
        }
        return null;
    }

    private void check(Service service) {
        if (getByName(service.getName()) != null) {
            throw new RuntimeException("重复创建服务:" + service.getName());
        }
    }

    private void initService(Service service) {
        try {
            service.setScriptEngine(scriptEngine);
            service.init();
        } catch (Exception e) {
            throw new CheckException(e);
        }
    }

    private void tryAdd(Service service) {

        check(service);
        if (service.getId() == null) {
            service.setId(IdStrategy.createId(service));
        }
        initService(service);
        services.add(service);
    }

    @Override
    public List<Service> listAll() {
        return services;
    }


    @Override
    public Service getById(String id) {

        for (Service service : services) {
            if (service.getId().equals(id)) {
                return service;
            }
        }

        return null;
    }

    @Override
    public void updateOrAdd(Service service) {

        if (service.getId() != null) {
            Service s = getById(service.getId());
            if (s == null) {
                throw new RuntimeException("不存在的服务:" + service.getId());
            }

            tryUpdate(service, s);
            serviceDao.update(service);
        } else {
            tryAdd(service);
            serviceDao.add(service);
        }
    }

    private void tryUpdate(Service service, Service old) {
        old.destroy();
        services.remove(old);
        try {
            service.setId(old.getId());
            tryAdd(service);
        } catch (Exception e) {
            old.init();
            services.add(old);
            throw e;
        }
    }

    @Override
    public void removeById(String id) {
        Service service = getById(id);
        if (service == null) {
            return;
        }
        service.destroy();
        services.remove(service);
        serviceDao.removeById(id);
    }

    @Override
    public void init() {
        List<Service> all = serviceDao.listAll();
        for (Service service : all) {
            try {
                tryAdd(service);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


}
